#include "GLScene.h"

GLScene::GLScene()
{
	GLProgram* program = GLProgram::ShareProgram();

	program->GenerateProgram(0, "mesh.vert", "mesh.frag", "mesh.geom");
	program->GenerateProgram(1, "curve.vert", "curve.frag");
	program->UseProgram(0);

	// directional light
	program->SetDirLight(
		{ 0.0f, -1.0f, 0.0f },
		{ 0.5f, 0.5f, 0.5f },
		{ 0.4f, 0.4f, 0.4f },
		{ 0.8f, 0.8f, 0.8f }
	);

	// point light
	program->SetPointLight(0,
		{ 0.0f, -10.0f, 10.0f },
		{ 0.5f, 0.5f, 0.5f },
		{ 0.4f, 0.4f, 0.4f },
		{ 0.8f, 0.8f, 0.8f }
	);

	program->SetPointLight(1,
		{ 0.0f, -10.0f, -10.0f },
		{ 0.5f, 0.5f, 0.5f },
		{ 0.4f, 0.4f, 0.4f },
		{ 0.8f, 0.8f, 0.8f }
	);
}

void GLScene::AddMesh(GLMeshPtr mesh, GLenum mode)
{
	m_meshes.push_back(mesh);
	m_modes.push_back(mode);
}

void GLScene::AddCurve(GLCurvePtr curve)
{
	m_curves.push_back(curve);
}

void GLScene::Draw() const
{
	GLProgram* program = GLProgram::ShareProgram();

	program->UseProgram(0);
	program->InitProgram();

	for (int i = 0; i < m_meshes.size(); i++)
	{
		glPolygonMode(GL_FRONT_AND_BACK, m_modes[i]);
		m_meshes[i]->Draw();
	}

	program->UseProgram(1);
	program->InitProgram();
	
	for (int i = 0; i < m_curves.size(); i++)
		m_curves[i]->Draw();
}

void GLScene::AddTexture(const char* path, int i)
{
	GLProgram* program = GLProgram::ShareProgram();

	program->UseProgram(0);
	program->GenerateTexture(i);
	program->BindTexture(i);
	program->LoadTexture(path);
}
